import { createRouter, createWebHistory } from 'vue-router'
import VueCookies from 'vue-cookies'
const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    {
      path: '/',
      name: 'Home',
      redirect: "/psychology/home",
      component: () => import('../views/Home.vue'),
      children: [
        {
          path: '/psychology/home',
          name: 'HomeIndex',
          component: () => import('../views/home/HomeIndex.vue'),
          meta: {
            needLogin: false,
          }
        },
        {
          path: '/psychology/test',
          name: 'Test',
          component: () => import('../views/home/Test.vue'),
          meta: {
            needLogin: false,
          }
        },
        {
          path: '/psychology/read/',
          name: 'ReadIndex',
          component: () => import('../views/home/PostList.vue'),
          meta: {
            needLogin: false,
          }
        },
        {
          path: '/psychology/read/:id',
          name: 'PostDetail',
          component: () => import('../views/home/PostDetail.vue'),
          meta: {
            needLogin: false,
          }
        },
        {
          path: '/psychology/councelor',
          name: 'CouncelorIndex',
          component: () => import('../views/home/CouncelorIndex.vue'),
          meta: {
            needLogin: false,
          }
        },
        {
          path: '/psychology/user',
          name: 'UserHome',
          component: () => import('../views/home/UserHome.vue'),
          meta: {
            needLogin: false,
          }
        }
      ]
    },
    {
      path: '/login',
      name: 'Login',
      component: () => import('../views/Login.vue'),
      meta: {
        needLogin: false,
      }
    },
    {
      path: '/psychology/backend',
      name: 'AdminLayout',
      component: () => import('../views/Layout.vue'),
      children: [
        {
          path: '/psychology/backend/category',
          name: 'CategoryList',
          component: () => import('../views/category/CategoryList.vue'),
        },
        {
          path: '/psychology/backend/carousel',
          name: 'CarouselList',
          component: () => import('../views/carousel/CarouselList.vue'),
        },
        {
          path: '/psychology/backend/user',
          name: 'UserList',
          component: () => import('../views/user/UserList.vue'),
        },
        {
          path: '/psychology/backend/councelor',
          name: 'CouncelorList',
          component: () => import('../views/user/CouncelorList.vue'),
        },
        {
          path: '/psychology/backend/share',
          name: 'ShareList',
          component: () => import('../views/post/ShareList.vue'),
        },
        {
          path: '/psychology/backend/appointment',
          name: 'AppointmentList',
          component: () => import('../views/appointment/AppointmentList.vue'),
        },
        {
          path: '/psychology/backend/violation',
          name: 'ViolationList',
          component: () => import('../views/violation/ViolationList.vue'),
        },
        {
          path: '/psychology/backend/home',
          name: 'AdminHomeIndex',
          component: () => import('../views/HomeIndex.vue'),
        },
      ]
    }
  ]
})

// 路由跳转
router.beforeEach((to, from, next) => {
  const userInfo = VueCookies.get("userInfo");
  if (!userInfo && to.path != "/login" && to.meta.needLogin == true) {
    router.push("/login");
  }
  next();
})

export default router
